package com.burgeon.backend.EnterpriseWeChat.rest.Brand;

import com.burgeon.backend.EnterpriseWeChat.entity.Product;
import com.burgeon.backend.EnterpriseWeChat.rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 购物车推荐
 */
public class getRecommandList extends RESTBase {
    private final static String RANGE = "10";//分页数量
    private String ip;
    private String account;
    private String password;

    public getRecommandList(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Product> getData(String product,int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(product,page), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Product> productInventories= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                Product Product = new Product();
                Product.setProductValue(jsonArray.getJSONArray(i).getString(0));
                Product.setProduct01Name(jsonArray.getJSONArray(i).getString(1));
                Product.setSales(jsonArray.getJSONArray(i).getString(2));

                Product.setPriceList(jsonArray.getJSONArray(i).getString(3));
                Product.setAveragePrice(jsonArray.getJSONArray(i).getString(4));
                Product.setDiscount(jsonArray.getJSONArray(i).getString(5));
                Product.setProductId(jsonArray.getJSONArray(i).getString(6));
                Product.setProductName(jsonArray.getJSONArray(i).getString(7));
                Product.setBrandName(jsonArray.getJSONArray(i).getString(8));

               productInventories.add(Product);
            }
        }

        System.err.println(productInventories.size());
        return productInventories;
    }

    private MultiValueMap Data(String product,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'M_PRODUCT01_ID;VALUE'");//商品名
        colums.add("'M_PRODUCT01_ID;NAME'");//款号
        colums.add("BUY_NO");//销量
        colums.add("PRICELIST");//吊牌价
        colums.add("AVG_PRICEACTUAL");//平均价格
        colums.add("AVG_DISCOUNT");//折扣
        colums.add("M_PRODUCT01_ID");//id
        colums.add("'M_PRODUCT01_ID;NAME'");//图片
        colums.add("'M_PRODUCT01_ID;M_DIM1_ID;ATTRIBNAME'");

        params.put("columns", colums);
        params.put("table", "APP_RETAILRELATION");//表名
        params.put("count", "true");
        params.put("range",RANGE);
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        expr1.put("column","'M_PRODUCT_ID;VALUE'");
        String a = "'"+product+"'";
        expr1.put("condition",a);

        expr2.put("column","'M_PRODUCT_ID;NAME'");
        String b = "'"+product+"'";
        expr2.put("condition",b);


        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);
        paramss.put("combine","or");

        HashMap orderby = new HashMap();

        orderby.put("column", "BUY_NO");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        if(!product.equals("null")){
            params.put("params", paramss);
        }

        System.err.println(params);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getRecommandList g = new getRecommandList("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
